package com.whosly.api.autoconfigure.holder;

import com.whosly.api.autoconfigure.data.IConfigData;
import lombok.Getter;

import java.util.HashMap;
import java.util.Map;

/**
 * 静态引用类，全局生效
 *
 * @param <T>
 */
public class AnnotationMapsHolder<T extends IConfigData> {
    private static final Map<String, IConfigData> maps = new HashMap<>();

    public static <T extends IConfigData> AnnotationMapsHolder<T> INSTANCE(String key){
        return new AnnotationMapsHolder<>(key);
    }

    @Getter
    private String key;

    public AnnotationMapsHolder(String key) {
        this.key = key;
    }

    public T get(){
        IConfigData configData = maps.get(key);

        if(configData == null){
            return null;
        }

        return (T) configData;
    }

    public void set(T configData){
        maps.putIfAbsent(key, configData);
    }

}
